<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$pageid = (int) get('page');


if(count($_POST)>0){
    while(ob_get_level()>0) ob_end_clean();
    $count = 0;
    foreach($_POST as $blockid => $jqueryExtra){
        $blockid = $DB->escape($blockid);
        if($DB->countRows($CONFIG->dbprefix.'blocks', "id = '{$blockid}' AND page_id = '{$pageid}' AND workspace_id = '{$USER->workspace}'")>0){
            $DB->update($CONFIG->dbprefix.'blocks', array('exec_order'=>$count), "id = '{$blockid}' AND page_id = '{$pageid}' AND workspace_id = '{$USER->workspace}'");
            $count++;
        }
    }
    JFX::addSuccess('Block Execution Order saved!');
    exit;
}







$VIEW->template = 'admin-thickbox.tpl';

$blocks = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}blocks WHERE page_id = {$pageid} ORDER BY exec_order ASC");


$VIEW->addJs('
$(document).ready(function(){
    var sortableStop = function(){

        var sorted = $("#jfx-block-exec-order").sortable("serialize");
        //alert(sorted);
        $("#jfx-block-exec-order").css("display", "none");
        $("#jfx-workspace-inner").get(0).innerHTML = \'<div align="center"><img src="'.$CONFIG->imageUrl.'/admin/loadingAnimation.gif" alt="Loading..." /></div>\';
        $.post("'.$CONFIG->adminUrl.'/ajax/block-execution-order/?page='.$pageid.'", sorted, JFXBlockExecutionOrderRedirect);
    }
    $("#jfx-block-exec-order").sortable({cursor : "pointer", stop : sortableStop });

});
function JFXBlockExecutionOrderRedirect(){
    window.location = window.location;
}
');

?>
<h1>Block Execution Order</h1>
<p>You can alter the order of execution for blocks on any page. This is handy because some blocks may depend on other blocks
being executed first.</p>

<?php

echo '<ul id="jfx-block-exec-order">';

foreach($blocks as $k=>$v){
    echo '<li id="'.$v['id'].'_'.$k.'">'.$LANG->getContent('core', 'block_title', $v['id']).'</li>';
}

echo '<ul>';














